package zxq.rollcall.course.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import zxq.rollcall.course.controller.action.IUserController;
import zxq.rollcall.course.entity.SystemUserTableEntity;
import zxq.rollcall.course.entity.dto.ResultDTO;
import zxq.rollcall.course.entity.dto.set.UserDTO;
import zxq.rollcall.course.entity.query.LoadQuery;
import zxq.rollcall.course.entity.query.StudentQuery;
import zxq.rollcall.course.entity.query.TeacherQuery;
import zxq.rollcall.course.service.MPSystemGradeTableService;
import zxq.rollcall.course.service.MPSystemUserTableService;

/**
 * <p>
 * 系统用户表 前端控制器
 * </p>
 *
 * @author Yanghu
 * @since 2020-01-22
 */
@Controller
@RequestMapping("/course/api/user")
@Api(tags = "系统用户表操作")
public class SystemUserTableAction implements IUserController {

    @Autowired
    MPSystemUserTableService mpSystemUserTableService;


    @Override
    public ResultDTO<Boolean> insertStudent(StudentQuery studentQuery) {
        Boolean rst = mpSystemUserTableService.saveStudentInfo(studentQuery);
        if(rst) {
            return new ResultDTO<>(1,"插入成功",true);
        }
        return new ResultDTO<>(0,"插入失败",false);
    }

    @Override
    public ResultDTO<Boolean> updateStudent(Integer id, StudentQuery studentQuery) {
        Boolean rst =  mpSystemUserTableService.updateStudentInfo(id,studentQuery);
        if(rst) {
            return new ResultDTO<>(1,"更新成功",true);
        }
        return new ResultDTO<>(0,"更新失败",false);
    }

    @Override
    public ResultDTO<Boolean> insertTeacher(TeacherQuery teacherQuery) {
        Boolean rst = mpSystemUserTableService.saveTeacherInfo(teacherQuery);
        if(rst) {
            return new ResultDTO<>(1,"插入成功",true);
        }
        return new ResultDTO<>(0,"插入失败",false);
    }

    @Override
    public ResultDTO<Boolean> updateTeacher(Integer id, TeacherQuery teacherQuery) {
        Boolean rst = mpSystemUserTableService.updateTeacherInfo(id,teacherQuery);
        if(rst) {
            return  new ResultDTO<>(1,"更新成功",true);
        }
        return new ResultDTO<>(0,"更新失败",false);
    }

    @Override
    public ResultDTO<Boolean> deleteUser(Integer id) {
        Boolean rst = mpSystemUserTableService.removeById(id);
        if(rst) {
            return new ResultDTO<>(1,"删除成功",true);
        }
        return new ResultDTO<>(0,"删除失败",false);
    }

    @Override
    public ResultDTO<UserDTO> queryUserInfo(Integer userId) {
        SystemUserTableEntity systemUserTableEntity = mpSystemUserTableService.getById(userId);
        if(systemUserTableEntity != null) {
            UserDTO userDTO = new UserDTO();
            BeanUtils.copyProperties(systemUserTableEntity, userDTO);
            switch (systemUserTableEntity.getRoleId()) {
                case student:
                    userDTO.setRoleId(1);
                    break;
                case teacher:
                    userDTO.setRoleId(2);
                    break;
            }
            return new ResultDTO<>(1,"查询成功",userDTO);
        }
        return new ResultDTO<>(0,"查询失败",null);
    }

    @Override
    public ResultDTO<UserDTO> load(LoadQuery query) {
        System.out.println("user:" + query.getUser() +" password:" + query.getPassword());
        SystemUserTableEntity systemUserTableEntity = new SystemUserTableEntity();
        systemUserTableEntity.setUserName(query.getUser());
        systemUserTableEntity.setUserPassword(query.getPassword());
        QueryWrapper<SystemUserTableEntity> queryWrapper = new QueryWrapper<>(systemUserTableEntity);
        systemUserTableEntity = mpSystemUserTableService.getOne(queryWrapper);
        if(systemUserTableEntity != null) {
            UserDTO userDTO = new UserDTO();
            BeanUtils.copyProperties(systemUserTableEntity,userDTO);
            switch (systemUserTableEntity.getRoleId()) {
                case student:
                    userDTO.setRoleId(1);
                    break;
                case teacher:
                    userDTO.setRoleId(2);
                    break;
            }

            return new ResultDTO<>(1,"查询成功",userDTO);
        }
        return new ResultDTO<>(0,"查询失败",null);
    }

}

